package com.fingard.dsp.bank.directbank.czcb01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.reqrespobj.single.*;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.util.Date;

/**
 * @ClassName CZCB01SinglePay
 * @Author Zhengzy
 * @Date 2019/12/18 10:38
 * @Version 1.0
 **/
public class CZCB01SinglePay extends CZCB01Base implements ISinglePay {
    @Override
    public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
        RetAtsSingle retAts = new RetAtsSingle(p_req1908.reqHeader);
        if (bankFront.userName == null || bankFront.userName == "") {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo("无客户号节点或未配置客户号！");
            retAts.writeEnd();
            return retAts;
        }
        RetAtsSglDetail detail = new RetAtsSglDetail();
        //Connection conn = FGBiz.getLocalDbConn();
        try {
            // 组装上送报文
            XElement stream = new XElement("stream", "");// 内层明文
            XmlTextWriter outTextWriter = new XmlTextWriter();// 外层报文

            outTextWriter.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"GBK\"?>");
            outTextWriter.writeStartElement("CZBANK");
            double payAmount = Double.parseDouble(p_req1908.singleDetail.payAmount);
            boolean tmpIsEBank = false;// 网银互联跨行付
            if ("1".equals(getBankConfig().getValueAsString("IsEBank").trim()) || "true".equals(getBankConfig().getValueAsString("IsEBank").trim().toLowerCase())) {
                double tmpEBankAmtLimit = 0;
                String tmpLimit = getBankConfig().getValueAsString("IsEBank");
                if (tmpLimit != null) {
                    tmpEBankAmtLimit = Double.parseDouble(tmpLimit);
                }
                if (tmpEBankAmtLimit <= 0 || payAmount <= tmpEBankAmtLimit) {
                    tmpIsEBank = true;
                }
            }
            if (tmpIsEBank) {
                if ("true".equals(getBankConfig().getValueAsString("IsEBank").trim().toLowerCase())) {
                    stream.addChild(new XElement("payType", "05"));// 支付方式 网银跨行支付
                } else if (payAmount > 50000 || "1".equals(p_req1908.singleDetail.paySpd)) {
                    stream.addChild(new XElement("payType", "02"));// 支付方式 大额支付
                } else {
                    stream.addChild(new XElement("payType", "03"));// 支付方式 小额支付
                }
            }
            stream.addChild(new XElement("action", "BC0201"));
            stream.addChild(new XElement("userName", bankFront.org));// 客户号
            stream.addChild(new XElement("clientID", p_req1908.singleDetail.askNumber));// 客户流水号
            Date tmpDateNow = new Date();
            stream.addChild(new XElement("payDate", Format.Date8Format.format(tmpDateNow)));// 付款日期
            stream.addChild(new XElement("payTime", Format.Time6Format.format(tmpDateNow)));// 付款时间
            stream.addChild(new XElement("recBankNo", ""));// -收款人所属银行行号 varchar(12)，可空，支付方式为02、03、05 (网银跨行支付)时非空； 06填swift code
            stream.addChild(new XElement("payAccountNo", p_req1908.singleDetail.payAct));// 付款账号
            stream.addChild(new XElement("recAccountNo", p_req1908.singleDetail.recAct));// 收款账号
            stream.addChild(new XElement("recAccountName", p_req1908.singleDetail.recName));// 收款账户名称
            stream.addChild(new XElement("recOpenBankName", p_req1908.singleDetail.cnapsName));// 收款账户开户行名
            stream.addChild(new XElement("recOpenBankCode", p_req1908.singleDetail.cnapsCode));// 收款账户开户行联行网点号
            String privateFlag = "1".equals(p_req1908.singleDetail.isPrivate) ? "0" : "1";
            stream.addChild(new XElement("privateFlag", privateFlag));// 公私标志 0：对私；1：对公
            stream.addChild(new XElement("tranAmount", p_req1908.singleDetail.payAmount));// 金额
            stream.addChild(new XElement("abstract", p_req1908.singleDetail.memo));// 摘要
            stream.addChild(new XElement("memo", p_req1908.singleDetail.memo));// 备注


            // 对明文报文进行加签加密  对xmlTextWriter进行加签加密
            //String encryptData = stream.加签加密;

            outTextWriter.writeXElement(stream); // 把明文组装入报文
            outTextWriter.writeElementString("signData", "加签加密内容");// 把密文组装入报文
            outTextWriter.writeEndElement();

            // 发送银行
            String[] bankResult = sendToBank(outTextWriter.toXmlString());
//            String[] bankResult = new String[]{"", "<?xml version=\"1.0\" encoding=\"GBK\"?>\n" +
//                    "<CZBANK>\n" +
//                    "<stream>\n" +
//                    "<status>0000</status>\n" +
//                    "\t\t<statusText>单笔支付</statusText>\n" +
//                    "\t\t<tranDate>20200103</tranDate>\n" +
//                    "\t\t<tranTime>151212</tranTime>\n" +
//                    "<payAccountNo>4242424</payAccountNo>\n" +
//                    "<payAccountName>洋洋账号名称</payAccountName>\n" +
//                    "<payOpenBankName>洋洋开户行</payOpenBankName>\n" +
//                    "<recAccountNo>987</recAccountNo>\n" +
//                    "<recAccountName>收款账户名称</recAccountName>\n" +
//                    "<recOpenBankName>收款账号开户行</recOpenBankName>\n" +
//                    "<tranAmount>17000.00</tranAmount>\n" +
//                    "<abstract>摘要</abstract>\n" +
//                    "</stream>\n" +
//                    "<signData>密文</signData>\n" +
//                    "</CZBANK>\n"};
            if (bankResult[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRetCode = tmpBkReader.getSimpleText("status");// 交易状态
                    String tmpRetInfo = tmpBkReader.getSimpleText("statusText");// 交易状态信息
                    if ("0000".equals(tmpRetCode)) {
                        detail.respCode = "0";
                        detail.respInfo = tmpRetInfo;
                        detail.transState = "2";// "2"代表未知
                        detail.payInfoCode = "2";
                        detail.transAmt = tmpBkReader.getSimpleText(tmpBkReader.getSimpleText("tranAmount"));
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpRetInfo;
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpSbMsg.toString();
                }
            } else {
                detail.respCode = "-1";
                detail.respInfo = bankResult[1];
            }
        } catch (Exception e) {
            detail.respCode = "-1";
            detail.respInfo = e.getMessage();
        }
        retAts.writeRetDetail(detail);
        return retAts;
    }

    @Override
    public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {
        RetAtsSingle retAts = new RetAtsSingle(p_req1944.reqHeader);
        if (bankFront.userName == null || bankFront.userName == "") {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo("无客户号节点或未配置客户号！");
            retAts.writeEnd();
            return retAts;
        }
        RetAtsSglDetail detail = new RetAtsSglDetail();
        try {
            // 组装上送报文
            XElement stream = new XElement("stream", "");// 内层明文
            XmlTextWriter outTextWriter = new XmlTextWriter();// 外层报文

            outTextWriter.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"GBK\"?>");
            outTextWriter.writeStartElement("CZBANK");

            stream.addChild(new XElement("action", "BC0104"));
            stream.addChild(new XElement("userName", bankFront.org));
            stream.addChild(new XElement("clientID", p_req1944.askNumber));

            // 对明文报文进行加签加密  对xmlTextWriter进行加签加密
            //String encryptData = stream.加签加密;

            outTextWriter.writeXElement(stream); // 把明文组装入报文
            outTextWriter.writeElementString("signData", "加签加密内容");// 把密文组装入报文
            outTextWriter.writeEndElement();


            // 发送给银行
            String[] bankResult = sendToBank(outTextWriter.toXmlString());
//            String[] bankResult = new String[]{"", "<?xml version=\"1.0\" encoding=\"GBK\"?>\n" +
//                    "<CZBANK>\n" +
//                    "<stream>\n" +
//                    "<status>0000</status>0\n" +
//                    "<statusText>交易状态信息</statusText>0\n" +
//                    "<list name=\"userDataList\">\n" +
//                    "<row>\n" +
//                    "<stt>4</stt>\n" +
//                    "<statusText>原交易状态信息含义：撤销</statusText>\n" +
//                    "</row>\n" +
//                    "</list>\n" +
//                    "</stream>\n" +
//                    "<signData>密文</signData>\n" +
//                    "</CZBANK>\n"};
            if (bankResult[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRetCode = tmpBkReader.getSimpleText("status");// 交易状态
                    String tmpRetInfo = tmpBkReader.getSimpleText("statusText");// 交易状态信息
                    if ("0000".equals(tmpRetCode)) {
                        detail.respCode = "0";
                        detail.respInfo = tmpRetInfo;
                        // 状态标志  0成功 1失败 2未知 3审核拒绝 4用户撤销
                        detail.transState = tmpBkReader.getSimpleText("stt");
                        detail.payInfoCode = tmpBkReader.getSimpleText("stt");
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpRetInfo;
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = tmpSbMsg.toString();
                }
            } else {
                detail.respCode = "-1";
                detail.respInfo = bankResult[1];
            }
        } catch (Exception e) {
            detail.respCode = "-1";
            detail.respInfo = e.getMessage();
        }
        retAts.writeRetDetail(detail);
        return retAts;
    }

    @Override
    public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
        return null;
    }

    @Override
    public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
        return null;
    }
}
